﻿using System;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using Telerik.Web.UI;
using Thuoc24h.BasePage;
using Thuoc24h.Business;
using Thuoc24h.Common;
using Thuoc24h.Data;

namespace Thuoc24h.AdminCP.Controls
{
    public partial class CategoryEdit : BaseUserControls
    {


        private int id;
        private Category obj = new Category();

        [Description("Category Type Id")]
        [Browsable(true)]
        [DefaultSettingValue("")]
        public int CategoryTypeId
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Convert.ToInt32(Request.QueryString["ID"]);
            if (!Page.IsPostBack)
            {
                FillDataListBoxRoot();
                if (id > 0)
                {
                    btnSave.Text = Common.ActionNames.Update;
                    BindData();
                }
                else
                {
                    btnSave.Text = Common.ActionNames.AddNew;
                }

                cbbCategoryType.SelectedValue = CategoryTypeId.ToString();

            }

        }


        public void FillDataListBoxRoot()
        {
            radTreeCategories.DataSource = ReviewRepository.GetCategoryParent(CategoryTypeId);
            radTreeCategories.DataValueField = "Id";
            radTreeCategories.DataTextField = "Name";
            radTreeCategories.DataFieldParentID = "ParentId";
            radTreeCategories.DataFieldID = "Id";
            radTreeCategories.DataBind();            
        }

        protected void radTreeCategories_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            hfCategoryId.Value = e.Node.Value;

        }


        private void GetData()
        {

            obj.Name = txtName.Text;
            obj.UnsignName = txtUnsignName.Text;
            obj.SortOrder = Convert.ToInt32(radNumberOrd.Text);
            obj.CategoryType = Convert.ToInt32(cbbCategoryType.SelectedValue);
            var categoryParentId = radTreeCategories.SelectedValue;
            if (categoryParentId != string.Empty)
            {
                obj.ParentId = categoryParentId.ToInt32Return0();
            }
            else
            {
                obj.ParentId = null;
            }
        }
        private void Reset()
        {

            txtName.Text = string.Empty;
            txtUnsignName.Text = string.Empty;
            radNumberOrd.MinValue = 1;
            cbbCategoryType.SelectedIndex = 0;
            radTreeCategories.Nodes.Clear();

        }
        private void SaveData()
        {
            if (id > 0)
            {
                obj = context.Categories.SingleOrDefault(item => item.Id == id);
                if (obj != null)
                {
                    GetData();
                }

            }
            else
            {
                GetData();
                context.Categories.AddObject(obj);
            }

            if (context.SaveChanges() > 0)
            {
                if (CategoryTypeId == 1)
                {
                    Response.Redirect(UrlManager.Admin.CategoryProductView);
                }
                else
                {
                    Response.Redirect(UrlManager.Admin.CategoryNewsView);
                }

            }
        }
        private void BindData()
        {

            obj = ReviewRepository.GetCategoryById(id);
            txtName.Text = obj.Name;
            txtUnsignName.Text = obj.UnsignName;
            radNumberOrd.Value = obj.SortOrder;
            if (obj.ParentId != null)
            {
                var parentId = obj.ParentId.Value;
                var node = radTreeCategories.FindNodeByValue(parentId.ToString());
                if (node != null)
                    node.Selected = true;
            }
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            SaveData();
        }

        protected void OnClick(object sender, EventArgs e)
        {
            Reset();
        }
    }
}